Option Explicit On
Option Strict On

Imports System.Collections

Imports nstl
Imports nstl.Exceptions
Imports NUnit.Framework


Namespace nstl.test
    '<TestFixture()> _
    Public Class ListTest
        ''' <summary>
        ''' Initialize() is called once during test execution before
        ''' test methods in this test class are executed.
        ''' </summary>
        <SetUp()> Public Sub Initialize()
            ' TODO: Add test initialization code
        End Sub

        ''' <summary>
        ''' Cleanup() is called once during test execution after
        ''' test methods in this class have executed unless the
        ''' corresponding Initialize() call threw an exception.
        ''' </summary>
        <TearDown()> _
        Public Sub Cleanup()
            ' TODO: Add test cleanup code
        End Sub

        <Test()> _
        Public Sub insert()
            Dim l As New DList(Of Integer)
            For i As Integer = 0 To 9
                l.Insert(l.Begin(), i)
            Next

            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}))


            Dim l2 As New DList(Of Integer)
            For i As Integer = 0 To 9
                l2.Insert(l2.End(), i)
            Next
            Assert.IsFalse(TestUtil.mismatch(l2, New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}))

            Dim it As DList(Of Integer).Iterator = l2.Begin()
            it = NStlUtil.Advance(Of Integer, DList(Of Integer).Iterator)(it, 3)
            l2.Insert(it, 4)
            Assert.IsFalse(TestUtil.mismatch(l2, New Integer() {0, 1, 2, 4, 3, 4, 5, 6, 7, 8, 9}))


            Dim v1 As New Vector(Of Integer)(New Integer() {0, 1, 2, 3, 4, 5, 6, 7})

            it = l2.Begin()
            it = NStlUtil.Advance(Of Integer, DList(Of Integer).Iterator)(it, 3)
            l2.Insert(it, v1.Begin, v1.End)
            Assert.IsFalse(TestUtil.mismatch(l2, New Integer() {0, 1, 2, 0, 1, 2, 3, 4, 5, 6, 7, 4, 3, 4, 5, 6, 7, 8, 9}))

            l.Insert(NStlUtil.Advance(Of Integer, DList(Of Integer).Iterator)(l.Begin(), 2), 4, 5)
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {9, 8, 5, 5, 5, 5, 7, 6, 5, 4, 3, 2, 1, 0}))
        End Sub
        <Test()> _
        Public Sub assign()
            Dim v As New Vector(Of Integer)(New Integer() {0, 1, 2, 3, 4, 5})
            Dim l As New DList(Of Integer)
            For i As Integer = 0 To 9
                l.Insert(l.Begin(), i)
            Next
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}))

            l.Assign(v.Begin(), v.End())
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {0, 1, 2, 3, 4, 5}))

        End Sub
        <Test()> _
        Public Sub push_XXX()
            Dim l As New DList(Of Integer)
            For i As Integer = 0 To 9
                l.PushFront(i)
            Next
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}))
            Dim l2 As New DList(Of Integer)
            For i As Integer = 0 To 9
                l2.PushBack(i)
            Next
            Assert.IsFalse(TestUtil.mismatch(l2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
        End Sub
        <Test()> _
        Public Sub front_back()
            Dim l As New DList(Of Integer)
            For i As Integer = 0 To 9
                l.PushFront(i)
            Next
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}))

            Assert.AreEqual(l.Front, 9)
            Assert.AreEqual(l.Back, 0)

            Dim l2 As New DList(Of Integer)
            Try
                l2.Front()
                Assert.Fail()
            Catch ex As ContainerEmptyException
            End Try

            Try
                l2.Back()
                Assert.Fail()
            Catch ex As ContainerEmptyException
            End Try
        End Sub
        <Test()> _
        Public Sub erase_it()
            Dim l As New DList(Of Integer)
            For i As Integer = 0 To 9
                l.PushFront(i)
            Next
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}))

            Assert.IsTrue(CType(l.Erase(l.Begin(), NStlUtil.Advance(Of Integer, DList(Of Integer).Iterator)(l.Begin(), 3)).Value, Integer) = 6)
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {6, 5, 4, 3, 2, 1, 0}))

            Assert.IsTrue(CType(l.Erase(NStlUtil.Advance(Of Integer, DList(Of Integer).Iterator)(l.Begin(), 3)).Value, Integer) = 2)
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {6, 5, 4, 2, 1, 0}))

        End Sub
        <Test()> _
        Public Sub ctors()
            Dim l As New DList(Of Integer)(New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}))

            Dim v As New Vector(Of Integer)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
            Dim l2 As New DList(Of Integer)(v.Begin, v.End)
            Assert.IsFalse(TestUtil.mismatch(l2, New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}))

            Dim l3 As New DList(Of Integer)(3)
            Assert.IsFalse(TestUtil.mismatch(l3, 0, 0, 0))

            Dim l4 As New DList(Of Integer)(3, 5)
            Assert.IsFalse(TestUtil.mismatch(l4, 5, 5, 5))

            Dim l5 As New DList(Of Integer)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}))
        End Sub
        <Test()> _
        Public Sub pop_XXX()
            Dim l As New DList(Of Integer)(New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
            l.PopFront()
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9}))

            l.PopBack()
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {1, 2, 3, 4, 5, 6, 7, 8}))

        End Sub
        <Test()> _
        Public Sub resize()
            Dim l As New DList(Of Integer)(New Integer() {0, 1, 2, 3, 4})
            l.Resize(7, 5)
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {0, 1, 2, 3, 4, 5, 5}))
            l.Resize(9)
            Assert.IsFalse(TestUtil.mismatch(l, New Integer() {0, 1, 2, 3, 4, 5, 5, 0, 0}))
        End Sub

        <Test()> _
        Public Sub splice()
            Dim c1 As New DList(Of Integer)(New Integer() {10, 11})
            Dim c2 As New DList(Of Integer)(New Integer() {12, 20, 21})
            Dim c3 As New DList(Of Integer)(New Integer() {30, 31})
            Dim c4 As New DList(Of Integer)(New Integer() {40, 41, 42})

            Dim w_Iter As DList(Of Integer).Iterator = NStlUtil.Advance(Of Integer, DList(Of Integer).Iterator)(c2.Begin(), 1)
            c2.Splice(w_Iter, c1)
            Assert.IsFalse(TestUtil.mismatch(c2, New Integer() {12, 10, 11, 20, 21}))

            Dim f_Iter As DList(Of Integer).Iterator = c3.Begin()
            c2.Splice(w_Iter, c3, f_Iter)
            Assert.IsFalse(TestUtil.mismatch(c2, New Integer() {12, 10, 11, 30, 20, 21}))

            f_Iter = c4.Begin()
            Dim l_Iter As DList(Of Integer).Iterator = c4.End()
            l_Iter = NStlUtil.Advance(Of Integer, DList(Of Integer).Iterator)(l_Iter, -1)
            c2.Splice(w_Iter, c4, f_Iter, l_Iter)
            Assert.IsFalse(TestUtil.mismatch(c2, New Integer() {12, 10, 11, 30, 40, 41, 20, 21}))
        End Sub
        <Test()> _
    Public Sub swap()
            Dim c1 As New DList(Of Integer)(New Integer() {10, 11})
            Dim c2 As New DList(Of Integer)(New Integer() {12, 20, 21})

            c1.Swap(c2)
            Assert.IsFalse(TestUtil.mismatch(c1, New Integer() {12, 20, 21}))
            Assert.IsFalse(TestUtil.mismatch(c2, New Integer() {10, 11}))

        End Sub

        <Test()> _
        Public Sub enumerator()
            Dim l As New DList(Of Integer)(New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
            Dim v As New Vector(Of Integer)
            For Each cur As Integer In l
                v.PushBack(cur)
            Next
            Assert.IsFalse(TestUtil.mismatch(v, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
        End Sub
    End Class
End Namespace



